package com.otdemo.web;

import com.otdemo.dao.RestContentDao;
import com.otdemo.dao.RestScoreDao;
import com.otdemo.dao.RestUserDao;
import com.otdemo.entity.RestContent;
import com.otdemo.entity.RestScore;
import com.otdemo.entity.RestUser;
import com.otdemo.resp.CommonParam;
import com.otdemo.resp.RestScoreResp;
import com.otdemo.resp.RestUserResp;
import com.otdemo.type.GenderEnum;
import com.otdemo.type.RoleEnum;
import com.otdemo.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@WebServlet("/score/list")
public class ScoreController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//2.request和response的编码方法设置，一律UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        req.setAttribute("list", initList(new CommonParam(), req));
        req.getRequestDispatcher("/Score.jsp").forward(req, resp);
    }

    private List<RestScoreResp> initList(CommonParam param, HttpServletRequest request) {
        List<RestScore> list = new RestScoreDao().list();
        if (StringUtils.isNotEmpty(param.getUser())) {
            list = list.stream().filter(p -> p.getUser().equals(param.getUser().trim())).collect(Collectors.toList());
        }
        List<RestContent> contentList = new RestContentDao().list();
        List<RestUser> userList = new RestUserDao().list();
        Map<String, RestUser> userMap = userList.stream().collect(Collectors.toMap(RestUser::getUid, restUser -> restUser));
        Map<String, RestContent> contentMap = contentList.stream().collect(Collectors.toMap(RestContent::getUid, restBook -> restBook));
        List<RestScoreResp> respList = new ArrayList<>();
        try {
            for (RestScore rest : list) {
                RestScoreResp resp = new RestScoreResp();
                resp.setUid(rest.getUid());
                resp.setId(rest.getId());
                resp.setScore(rest.getScore());
                RestContent restContent = contentMap.get(rest.getContent());
                if (restContent == null) {
                    continue;
                }
                resp.setContentName(restContent.getName());
                RestUser restUser = userMap.get(rest.getUser());
                if (restUser == null) {
                    continue;
                }
                resp.setUserName(restUser.getName());
                resp.setNumber(StringUtils.substring(rest.getUid(), 0, 8).toUpperCase());
                respList.add(resp);
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return respList;
    }
}
